package yz.dut.edu.graduate.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yz.dut.edu.graduate.bean.Result;
import yz.dut.edu.graduate.dao.DishMapper;
import yz.dut.edu.graduate.pojo.Dish;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishService {
    @Autowired
    DishMapper dishMapper;

    public Result Push(Dish dish){
        Result result=new Result();
        Dish baseDish=new Dish();
        try {
            baseDish=dishMapper.selectByDishName(dish.getDishName());
            if(baseDish==null){
                dishMapper.insertSelective(dish);
                result.setDetails(dish.getdId());
                result.setSuccess(true);
                result.setMsg("菜品入库成功");
            }else{
                result.setMsg("菜品已在库中");
            }
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public int SearchByDishName(String dishName){
        Dish dish=new Dish();
        try{
            dish=dishMapper.selectByDishName(dishName);
            if(dish!=null){
                return dish.getdId();
            }else{
                return -1;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return -1;
    }

    public List<Dish> SelectByDishName(String dishName){
        List<Dish> res=new ArrayList<>();
        try{
            dishName="%"+dishName+"%";
            res=dishMapper.selectByDishNameRoughly(dishName);
        }catch(Exception e){
            e.printStackTrace();
        }
        return res;
    }

    public List<Dish> SelectAll(){
        return dishMapper.selectAll();
    }

    public PageInfo<Dish> SelectPage(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Dish> res=dishMapper.selectPage();
        PageInfo<Dish> pageInfo=new PageInfo<>(res);
        return pageInfo;
    }


    public boolean DeleteDish(int d_id){
        try {
            dishMapper.deleteByPrimaryKey(d_id);
            return true;
        }catch(Exception e){
            e.printStackTrace();
        return false;
    }
}

    public int DishOff(int d_id,float off){
        int InfluencedRows=0;
        Dish dish=new Dish();
        dish.setdId(d_id);
        dish.setDiscount(off);
        try{
            InfluencedRows=dishMapper.updateByPrimaryKeySelective(dish);
        }catch(Exception e){
            e.printStackTrace();
        }
        return InfluencedRows;
    }
    public int DishPriceAndOff(int d_id,float price,int state){
        int InfluencedRows=0;
        Dish dish=new Dish();
        dish.setdId(d_id);
        dish.setPrice(price);
        dish.setState(state);
        try{
            InfluencedRows=dishMapper.updateByPrimaryKeySelective(dish);
        }catch(Exception e){
            e.printStackTrace();
        }
        return InfluencedRows;
    }
}
